package com.xxg.imagemanager.service;

import com.xxg.imagemanager.baidu.BosManager;
import com.xxg.imagemanager.baidu.ImageRetrieval;
import com.xxg.imagemanager.domain.result.BaseResult;
import com.xxg.imagemanager.domain.result.ImageObject;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 图片管理接口实现
 *
 * @author xuxiaogang
 * @date 2021-12-09
 */
@Service
public class ImageManagerServiceImpl implements ImageManagerService {
    private final BosManager bosManager;
    private final ImageRetrieval imageRetrieval;

    public ImageManagerServiceImpl(BosManager bosManager, ImageRetrieval imageRetrieval) {
        this.bosManager = bosManager;
        this.imageRetrieval = imageRetrieval;
    }

    @Async("uploadImagesExecutor")
    @Override
    public void batchUploadImage() {
        bosManager.batchUploadImage();
    }

    @Async("createImageIndexExecutor")
    @Override
    public void batchCreateImageIndex() {
        List<ImageObject> images = bosManager.listAllKeys();
        imageRetrieval.batchCreateImageIndex(images);
    }

    @Override
    public BaseResult<String> searchByPic(byte[] file) {
        imageRetrieval.searchByPic(file);
        return null;
    }

    @Override
    public BaseResult<String> check(String fileName) {
        return bosManager.check(fileName);
    }

    @Override
    public BaseResult<String> count() {
        List<ImageObject> keys = bosManager.listAllKeys();
        return new BaseResult<>(String.valueOf(keys.size()));
    }
}
